package com.system.user.controller;

import com.system.common.bean.Admin;
import com.system.common.service.IAdminService;
import com.system.common.util.PasswordUtil;
import com.system.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @DubboReference
    private IAdminService adminService;

    @PostMapping("/add")
    public Result add(@RequestBody Admin admin){
        admin.setPassword(PasswordUtil.getEncryption(admin.getPassword()));
        adminService.addAdmin(admin);
        return Result.ok("添加管理员成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        adminService.deleteAdmin(id);
        return Result.ok("删除管理员成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        adminService.updateAdmin(admin);
        return Result.ok("修改管理员成功");
    }

    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody Admin admin){
        adminService.updateStatus(admin.getId(), admin.getStatus());
        return Result.ok("修改管理员状态成功");
    }

    @GetMapping("/list")
    public Result list(){
        return Result.ok("获取管理员列表成功",adminService.getAdminList());
    }

    @GetMapping("/page")
    public Result page(Admin admin, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        return Result.ok(adminService.getPage(admin, new Page<>(pageNum, pageSize)));
    }

    @GetMapping("/getAdminById/{id}")
    public Result getAdminById(@PathVariable Integer id){
        return Result.ok("获取管理员成功",adminService.getAdminById(id));
    }

    @GetMapping("/getAdminByUsername/{username}")
    public Result getAdminByUsername(@PathVariable String username){
        return Result.ok("获取管理员成功",adminService.getAdminByUsername(username));
    }
}

